package com.site.wisdom_construction.controller.publicModule;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.dao.safetyModule.SafetyDangerStatisticsDao;
import com.site.wisdom_construction.entity.iotModule.HighSupportsRecord;
import com.site.wisdom_construction.entity.personnelModule.Project;
import com.site.wisdom_construction.entity.qualityModule.QualityDangerStatistics;
import com.site.wisdom_construction.service.iotModule.HighSupportsRecordService;
import com.site.wisdom_construction.service.iotModule.HighSupportsService;
import com.site.wisdom_construction.service.iotModule.TowerCraneService;
import com.site.wisdom_construction.service.personnelModule.*;
import com.site.wisdom_construction.service.qualityModule.QualityDangerRecordService;
import com.site.wisdom_construction.service.qualityModule.QualityDangerStatisticsService;
import com.site.wisdom_construction.service.safetyModule.SafetyDangerRecordService;
import com.site.wisdom_construction.service.safetyModule.SafetyDangerStatisticsService;
import com.site.wisdom_construction.service.scheduleModule.ProjectProgressPlanService;
import com.site.wisdom_construction.util.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/** @Author daiyifei @Date 2022/3/8 @Description */
@EnableScheduling
@Lazy(false)
@Component
public class TimedTaskController {
  @Autowired private HighSupportsService highSupportsService;

  @Autowired private HighSupportsRecordService highSupportsRecordService;

  @Autowired private ProjectService projectService;

  @Autowired private ContractWorkersService contractWorkersService;

  @Autowired private ContractWorkersStatisticsService contractWorkersStatisticsService;

  @Autowired private ManagerialStaffService managerialStaffService;

  @Autowired private ManagerialStaffStatisticsService managerialStaffStatisticsService;

  @Autowired private SafetyDangerRecordService safetyDangerRecordService;

  @Autowired private SafetyDangerStatisticsService safetyDangerStatisticsService;

  @Autowired private QualityDangerRecordService qualityDangerRecordService;

  @Autowired private QualityDangerStatisticsService qualityDangerStatisticsService;

  @Autowired private ProjectProgressPlanService projectProgressPlanService;

  @Autowired private TowerCraneService towerCraneService;

//  // 定时任务接口
//  @Scheduled(cron = "0 0/5 * * * ? ")
//  public void addTowerCrane() {
//    JSONObject jsonObject = new JSONObject();
//    HttpRequest httpRequest =new HttpRequest();
//    String path = "http://gdw-sz.taantech.com:9090/TowerService/Realtime";
//    jsonObject = httpRequest.StringData(path,"imei=TAKJ_T_20212076&apikey=%7B8D46E497-E6EB-473A-B38B-7288598CB13E%7D");
//    Map map = new HashMap();
//    map.put("projectId",1);
//    map.put("towerIMei",jsonObject.get("F_TowerIMEI"));
//    map.put("writeTime",jsonObject.get("F_WriteTime"));
//    map.put("weight",jsonObject.get("F_Weight"));
//    map.put("torque",jsonObject.get("F_Torque"));
//    map.put("torqueRate",jsonObject.get("F_TorqueRate"));
//    map.put("rotation",jsonObject.get("F_Rotation"));
//    map.put("range",jsonObject.get("F_Range"));
//    map.put("height",jsonObject.get("F_Height"));
//    map.put("windSpeed",jsonObject.get("F_WindSpeed"));
//    map.put("pitch",jsonObject.get("F_Pitch"));
//    map.put("roll",jsonObject.get("F_Roll"));
//    towerCraneService.insertTowerCrane(map);
//  }
//
//  // 定时任务接口
//  @Scheduled(cron = "0 0/3 * * * ? ")
//  public void addHighSupports() {
//    HttpRequest httpRequest = new HttpRequest();
//    String url = "http://platform.youshucloud.com/wis/api/data/curNodeData";
//    int[] d = {7515, 7516, 7517, 7518, 7519, 7520};
//    JSONObject jsonObject = new JSONObject();
//    jsonObject.put("appId", "zygeo");
//    jsonObject.put("appKey", "NlkhfHIYG378LJHhuNvelh8Gklhfq3Hv");
//    jsonObject.put("companyId", 205143);
//    jsonObject.put("projectId", 101194);
//    jsonObject.put("nodeIds", d);
//    String s = jsonObject.toString();
//    JSONObject jsonObjectR = new JSONObject();
//    jsonObjectR = httpRequest.StringDataJson(url, s);
//    // 传参Map
//    Map mapC = new HashMap();
//    // 获取其中的data
//    List list = (List) jsonObjectR.get("body");
//    Date date = new Date();
//    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
//    Map mapR = new HashMap();
//    mapR.put("projectId", 1);
//    mapR.put("sendTime", dateFormat.format(date));
//    Map mapB = new HashMap();
//    for (int i = 0; i < list.size(); i++) {
//      JSONObject json = (JSONObject) JSONObject.toJSON(list.get(i));
//      mapR.put("equipmentProjectId", json.getString("cid"));
//      if (json.getString("name").equals("倾斜X")) {
//        Double slantX = Double.parseDouble(json.getString("value"));
//        mapR.put("slantX", slantX);
//        int l = slantX.compareTo(-5.0);
//        int r = slantX.compareTo(5.0);
//        if (l < 0 || r > 0) {
//          mapB.put("measurePoint", json.getString("name"));
//          mapB.put("alarmValue", json.getString("value"));
//          mapB.put("equipmentId", json.getString("id"));
//        }
//
//      } else if (json.getString("name").equals("倾斜Y")) {
//        Double slantY = Double.parseDouble(json.getString("value"));
//        mapR.put("slantY", slantY);
//        int l = slantY.compareTo(-5.0);
//        int r = slantY.compareTo(5.0);
//        if (l < 0 || r > 0) {
//          mapB.put("measurePoint", json.getString("name"));
//          mapB.put("alarmValue", json.getString("value"));
//          mapB.put("equipmentId", json.getString("id"));
//        }
//      } else if (json.getString("name").equals("轴压")) {
//        Double axleLoad = Double.parseDouble(json.getString("value"));
//        mapR.put("axleLoad", axleLoad);
//        int l = axleLoad.compareTo(-0.1);
//        int r = axleLoad.compareTo(0.1);
//        if (l < 0 || r > 0) {
//          mapB.put("measurePoint", json.getString("name"));
//          mapB.put("alarmValue", json.getString("value"));
//          mapB.put("equipmentId", json.getString("id"));
//        }
//      } else if (json.getString("name").equals("位移")) {
//        Double displacement = Double.parseDouble(json.getString("value"));
//        mapR.put("displacement", displacement);
//        int l = displacement.compareTo(-100.0);
//        int r = displacement.compareTo(100.0);
//        if (l < 0 || r > 0) {
//          mapB.put("measurePoint", json.getString("name"));
//          mapB.put("alarmValue", json.getString("value"));
//          mapB.put("equipmentId", json.getString("id"));
//        }
//      } else if (json.getString("name").equals("温度")) {
//        Double temperature = Double.parseDouble(json.getString("value"));
//        mapR.put("temperature", temperature);
//        int l = temperature.compareTo(-15.0);
//        int r = temperature.compareTo(15.0);
//        if (l < 0 || r > 0) {
//          mapB.put("measurePoint", json.getString("name"));
//          mapB.put("alarmValue", json.getString("value"));
//          mapB.put("equipmentId", json.getString("id"));
//        }
//      } else if (json.getString("name").equals("电压")) {
//        Double voltage = Double.parseDouble(json.getString("value"));
//        mapR.put("voltage", voltage);
//        int l = voltage.compareTo(-5.0);
//        int r = voltage.compareTo(5.0);
//        if (l < 0 || r > 0) {
//          mapB.put("measurePoint", json.getString("name"));
//          mapB.put("alarmValue", json.getString("value"));
//          mapB.put("equipmentId", json.getString("id"));
//        }
//      } else {
//        return;
//      }
//    }
//    highSupportsService.insertHighSupports(mapR);
//    if (mapB.isEmpty()) {
//      return;
//    } else {
//      // 查询近三个小时是否有相同的数据
//      List<HighSupportsRecord> highSupportsRecordList =
//          highSupportsRecordService.findHighSupportsRecordByEquipmentId(mapB);
//
//      int as = highSupportsRecordList.size();
//      // 查询之后执行数据
//      mapB.put("Projectd", 7);
//      mapB.put("sendTime", dateFormat.format(date));
//      if (as > 0) {
//        return;
//      } else {
//        highSupportsRecordService.insertHighSupportsRecord(mapB);
//      }
//    }
//  }
//
//  /** 每月一号一点执行 */
//  @Scheduled(cron = "0 0 1 1 * ?")
//  public void timerToMonth() {
//    // 查询所有标段信息
//    List<Project> projectList = projectService.findProjectAll();
//    for (int i = 0; i < projectList.size(); i++) {
//      contractWorkersStatisticsService.deleteContractWorkersStatistics(projectList.get(i).getId());
//      managerialStaffStatisticsService.deleteManagerialStaffStatistics(projectList.get(i).getId());
//      safetyDangerStatisticsService.deleteSafetyDangerStatistics(projectList.get(i).getId());
//      qualityDangerStatisticsService.deleteQualityDangerStatistics(projectList.get(i).getId());
//    }
//  }
//
//  /** 每天早上2点执行,修改劳务人员趋势表人员数量(暂时改为四点执行未部署服务器) */
//  @Scheduled(cron = "0 0 2 * * ?")
//  public void timerToUpdateContractWorkersCount() {
//    // 查询所有标段信息
//    List<Project> projectList = projectService.findProjectAll();
//    for (int i = 0; i < projectList.size(); i++) {
//      Map map = new HashMap();
//      map.put("projectId", projectList.get(i).getId());
//      int count = contractWorkersService.countAllByProjectId(map);
//      map.put("population", count);
//      map.put("uploadTime", Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
//      contractWorkersStatisticsService.insertContractWorkersStatistics(map);
//    }
//  }
//
//  /** 每天早上2点执行,修改安管人员趋势表人员数量(暂时改为四点执行未部署服务器) */
//  @Scheduled(cron = "0 0 2 * * ?")
//  public void timerToUpdateManagerialStaffCount() {
//    // 查询所有标段信息
//    List<Project> projectList = projectService.findProjectAll();
//    for (int i = 0; i < projectList.size(); i++) {
//      Map map = new HashMap();
//      map.put("projectId", projectList.get(i).getId());
//      int count = managerialStaffService.countAllByProjectId(map);
//      map.put("population", count);
//      map.put("uploadTime", Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
//      managerialStaffStatisticsService.insertManagerialStaffStatistics(map);
//    }
//  }
//
//  /** 每天早上2点执行,修改安全管理趋势表隐患数量(暂时改为四点执行未部署服务器) */
//  @Scheduled(cron = "0 0 2 * * ?")
//  public void timerToUpdateSafetyDangerCount() {
//    // 查询所有标段信息
//    List<Project> projectList = projectService.findProjectAll();
//    for (int i = 0; i < projectList.size(); i++) {
//      Map map = new HashMap();
//      map.put("projectId", projectList.get(i).getId());
//      int count = safetyDangerRecordService.countAllByProjectId(map);
//      map.put("amount", count);
//      map.put("uploadTime", Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
//      safetyDangerStatisticsService.insertSafetyDangerStatistics(map);
//    }
//  }
//
//  /** 每天早上2点执行,修改质量管理趋势表人员数量(暂时改为四点执行未部署服务器) */
//  @Scheduled(cron = "0 0 2 * * ?")
//  public void timerToUpdateQualityDangerCount() {
//    // 查询所有标段信息
//    List<Project> projectList = projectService.findProjectAll();
//    for (int i = 0; i < projectList.size(); i++) {
//      Map map = new HashMap();
//      map.put("projectId", projectList.get(i).getId());
//      int count = qualityDangerRecordService.countAllByProjectId(map);
//      map.put("amount", count);
//      map.put("uploadTime", Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
//      qualityDangerStatisticsService.insertQualityDangerStatistics(map);
//    }
//  }
//
//  /** 每天早上一点执行,修改当前日期大于计划开工日期的项目状态 */
//  @Scheduled(cron = "0 0 1 * * ?")
//  public void timerToNow1() {
//    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//    Date now = new Date();
//    String time = format.format(now);
//    Map map = new HashMap();
//    map.put("nowTime", time);
//    projectProgressPlanService.updateStatus(map);
//  }
//
//  /** 每天早上3点执行,修改状态为1的项目实际天数 */
//  @Scheduled(cron = "0 0 3 * * ?")
//  public void timerToNow2() {
//    projectProgressPlanService.updateProjectPractical();
//  }
}
